package com.zhouyou.http.demo;

import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.cache.model.CacheMode;
import com.zhouyou.http.cache.model.CacheResult;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.ProgressDialogCallBack;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.demo.customapi.test1.ResultBean;
import com.zhouyou.http.demo.customapi.test1.TestApiResult1;
import com.zhouyou.http.demo.customapi.test10.ArticleBean;
import com.zhouyou.http.demo.customapi.test10.TestResultApi10;
import com.zhouyou.http.demo.customapi.test2.Result;
import com.zhouyou.http.demo.customapi.test2.TestApiResult2;
import com.zhouyou.http.demo.customapi.test3.TestApiResult3;
import com.zhouyou.http.demo.customapi.test4.GwclBean;
import com.zhouyou.http.demo.customapi.test9.FriendsListBean;
import com.zhouyou.http.demo.customapi.test9.MyResult;
import com.zhouyou.http.demo.customapi.testN.HttpManager;
import com.zhouyou.http.model.utils.ToastUtils;
import com.zhouyou.http.demo.view.ProgressDialog;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.subsciber.IProgressDialog;
import com.zhouyou.http.view.CancelableDialog;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;

import java.util.List;

import static com.zhouyou.http.demo.constant.AppConstant.*;

public class CustomApiAbility extends Ability {
    public String getResults() {
        return results;
    }

    private  String results="";

    public void setResults(String results) {
        this.results = results;
    }

    private IProgressDialog mProgressDialog = new IProgressDialog() {
        @Override
        public CancelableDialog getDialog() {
            ProgressDialog dialog = new ProgressDialog(CustomApiAbility.this);
            dialog.setMessage("请稍候...");
            return dialog;
        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_custom_api);
        findComponentById(ResourceTable.Id_btn_test_one).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onTestOne();
            }
        });
        findComponentById(ResourceTable.Id_btn_test_two).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onTestTwo();
            }
        });
        findComponentById(ResourceTable.Id_btn_test_three).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onTestThree();
            }
        });
        findComponentById(ResourceTable.Id_btn_test_four).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onTestFour();
            }
        });
        findComponentById(ResourceTable.Id_btn_test_five).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onTestFive();
            }
        });
        findComponentById(ResourceTable.Id_btn_test_six).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onTestSix();
            }
        });
        findComponentById(ResourceTable.Id_btn_test_N).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onTestN();
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    public void onTestOne() {
        EasyHttp.get(CUSTOMPATH)
                .baseUrl(new StringBuffer(SCHEMA).append(CUSTOMPATH_APIS).toString())
                .readTimeOut(30 * 1000)//局部定义读超时
                .writeTimeOut(30 * 1000)
                .connectTimeout(30 * 1000)
                .params("phone", "18688994275")
                .params("dtype", "json")
                .params("key", "5682c1f44a7f486e40f9720d6c97ffe4")
                .execute(new CallBackProxy<TestApiResult1<ResultBean>, ResultBean>(new ProgressDialogCallBack<ResultBean>(mProgressDialog) {
                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        results=e.getMessage();
                        ToastUtils.getInstance(CustomApiAbility.this).showToast(e.getMessage());
                    }

                    @Override
                    public void onSuccess(ResultBean response) {
                        results=response.toString();
                        if (response != null) ToastUtils.getInstance(CustomApiAbility.this).showToast(response.toString());
                    }
                }) {
                });
    }

    private void onTestTwo() {
        // java.net.UnknownServiceException: CLEARTEXT communication to v.juhe.cn not permitted by network security policy
        EasyHttp.get(new StringBuffer(SCHEMA).append(CUSTOMPATH_JAPI).append(CUSTOMPATH_JAPI_JOKE).toString())
                .params("key", "f5236a9fb8fc75fac0a4d9b8c27a4e90")
                .params("page", "1")
                .params("pagesize", "10")
                .params("sort", "asc")
                .params("time", "1418745237")
                .execute(new CallBackProxy<TestApiResult2<Result>, Result>(new ProgressDialogCallBack<Result>(mProgressDialog) {
                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        results=e.getMessage();
                        ToastUtils.getInstance(CustomApiAbility.this).showToast(e.getMessage());
                    }

                    @Override
                    public void onSuccess(Result result) {
                        results=result.toString();
                        if (result != null) ToastUtils.getInstance(CustomApiAbility.this).showToast(result.toString());
                    }
                }) {
                });
    }


    public void onTestThree() {
        /**
         * 注意域名    xxx.xx.xx 不对，同时在 config.json中修改
         * config.json
         */
        EasyHttp.post(new StringBuffer(SCHEMA).append(PATH_X).append(PATH_DLYBG_LOGIN).toString())
                //{"version":"2.5.0","phone":"13012463189","imei":"6789098763343","imsi":"6675343576887","gwkl":"dl_sj1192"}
                .upJson("{\"\":\"2.5.0\",\"\":\"\",\"\":\"\",\"\":\"\",\"gwkl\":\"\"}")
                .execute(new CallBackProxy<TestApiResult3<List<String>>, List<String>>(new ProgressDialogCallBack<List<String>>(mProgressDialog) {
                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        results="onError";
                        ToastUtils.getInstance(CustomApiAbility.this).showToast(e.getMessage());
                    }

                    @Override
                    public void onSuccess(List<String> result) {
                        results="onSuccess";
                        if (result != null) ToastUtils.getInstance(CustomApiAbility.this).showToast(result.toString());
                    }
                }) {
                });
    }


    public void onTestFour() {
        EasyHttp.post("api/")
                .baseUrl(new StringBuffer(SCHEMA).append(PATH_X).append(PATH_DLYBG).toString())
                .upJson("{\"\":\"\",\"\":\"\",\"\":\"\",\"\":\"\",\"\":\"\",\"version\":\"1.0.0\"}")
                .cacheMode(CacheMode.CACHEANDREMOTE)
                .cacheKey(this.getClass().getSimpleName() + "test")
                //方式一
                .execute(new CallBackProxy<TestApiResult3<CacheResult<List<GwclBean>>>, CacheResult<List<GwclBean>>>(new SimpleCallBack<CacheResult<List<GwclBean>>>() {
                    @Override
                    public void onError(ApiException e) {
                        results=e.getMessage();
                        ToastUtils.getInstance(CustomApiAbility.this).showToast(e.getMessage());
                    }

                    @Override
                    public void onSuccess(CacheResult<List<GwclBean>> cacheResult) {
                        String from;
                        if (cacheResult.isFromCache) {
                            from = "我来自缓存:";
                        } else {
                            from = "我来自远程网络:";
                        }
                        results=cacheResult.data.toString();
                        ToastUtils.getInstance(CustomApiAbility.this).showToast(from + cacheResult.data.toString());
                    }
                }) {
                });
        //方式二
        //如果你想获取当前的返回是否来自缓存需要在解析的数据外面包裹CacheResult<>  cacheResult.isFromCache可以获取是来自缓存还是网络
        //如果你不想关注是否来自缓存，可以不加CacheResult<>，就是上层没有isFromCache这个标记了
               /* .execute(new CallBackProxy<TestApiResult3<List<GwclBean>>, List<GwclBean>>(new SimpleCallBack<CacheResult<List<GwclBean>>>() {
                    @Override
                    public void onError(ApiException e) {
                        showToast(e.getMessage());
                    }

                    @Override
                    public void onSuccess(CacheResult<List<GwclBean>> result) {
                        HttpLog.i(">>>>>>>>>>>>>"+result.toString());
                        if (result != null) showToast(result.toString());
                    }
                }) {
                });*/
    }

    /**
     * 自定义请求返回集合
     */
    public void onTestFive() {
        EasyHttp.get(new StringBuffer(SCHEMA).append(KAITENG).append(FRINEDS).toString())
                .params("token", "5b305fbeaa331")
                .params("keyword", "")
                .execute(new CallBackProxy<MyResult<List<FriendsListBean>>, List<FriendsListBean>>(new SimpleCallBack<List<FriendsListBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        results=e.getMessage();
                        ToastUtils.getInstance(CustomApiAbility.this).showToast(e.getMessage());
                    }

                    @Override
                    public void onSuccess(List<FriendsListBean> result) {
                        results=result.toString();
                        if (result != null) ToastUtils.getInstance(CustomApiAbility.this).showToast(result.toString());
                    }
                }) {
                });
    }
    public void onTestSix(){
        EasyHttp.get(new StringBuffer(SCHEMA).append(WANOHOS).append(ARTICLE).toString())
                .execute(new CallBackProxy<TestResultApi10<ArticleBean>, ArticleBean>(new SimpleCallBack<ArticleBean>() {
                    @Override
                    public void onError(ApiException e) {
                        results=e.getMessage();
                        ToastUtils.getInstance(CustomApiAbility.this).showToast(e.getMessage());
                    }

                    @Override
                    public void onSuccess(ArticleBean result) {
                        results=result.toString();
                        if (result != null) ToastUtils.getInstance(CustomApiAbility.this).showToast(result.toString());
                    }
                }) {
                });
    }

    /**
     * 主要解决重复写代理的问题,用法不变，只是将EasyHttp换成你自己定义的HttpManager去访问。HttpManager只是个类名可以使用其他名称。
     *
     */
    public void onTestN(){
        HttpManager.get(new StringBuffer(SCHEMA).append(CUSTOMPATH_JAPI).append(CUSTOMPATH_JAPI_JOKE).toString())
                .params("key", "f5236a9fb8fc75fac0a4d9b8c27a4e90")
                .params("page", "1")
                .params("pagesize", "10")
                .params("sort", "asc")
                .params("time", "1418745237")
                .execute(new ProgressDialogCallBack<Result>(mProgressDialog) {//这么实现是不是没有代理了
                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        results="onError";
                        ToastUtils.getInstance(CustomApiAbility.this).showToast(e.getMessage());
                    }

                    @Override
                    public void onSuccess(Result result) {
                        results="onSuccess";
                        if (result != null) ToastUtils.getInstance(CustomApiAbility.this).showToast(result.toString());
                    }
                });
    }




}
